#ifndef SwitchWatcher_H
#define SwitchWatcher_H

#include <QObject>
#include <QTimer>

#include "ParallelPort.h"

class SwitchWatcher : public QObject
{
	Q_OBJECT
public:
	SwitchWatcher(int statusPin=13, QString device="/dev/parport0"); 
	~SwitchWatcher();

	bool isSwitchOn() { return m_isSwitchOn; }
	bool isActive() { return m_switchPollTimer.isActive(); }
	int pollTime() { return m_switchPollTimer.interval(); }
	
signals:
	void switchToggled(bool);
	
public slots:
	void releasePort();
	void takePort();
	void setPollTime(int);

private slots:
	void pollSwitch();

private:
	// Parallel port to monitor
	QString m_device;
	
	// Parallel port pin to watch
	int m_pin;
	
	// State flag
	bool m_isSwitchOn;	
	
	// Last state of the switch
	int m_lastSwitchState;
	
	// Port object used to read wall switch state
	ParallelPort m_port;
	
	// Status register read from parallel port
	volatile unsigned char m_statRReg;
	
	// Timer used to poll the switch
	QTimer m_switchPollTimer;
};


#endif
